package cn.stu.cms.service.impl;


import cn.stu.cms.dto.NegotiationPageDto;
import cn.stu.cms.dto.PageDto;
import cn.stu.cms.mapper.NegotiationMapper;
import cn.stu.cms.pojo.Customers;
import cn.stu.cms.pojo.Employees;
import cn.stu.cms.pojo.Negotiation;
import cn.stu.cms.pojo.Users;
import cn.stu.cms.service.CustomersService;
import cn.stu.cms.service.EmployeesService;
import cn.stu.cms.service.NegotiationService;
import cn.stu.cms.service.UsersService;
import cn.stu.cms.util.R;
import cn.stu.cms.util.UserHolder;
import cn.stu.cms.vo.EmployeeData;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import static cn.stu.cms.common.CommonConstants.*;

/**
 * (Negotiation)表服务实现类
 *
 * @author gh
 * @since 2023-07-21 16:09:55
 */
@Service
@AllArgsConstructor
public class NegotiationServiceImpl extends ServiceImpl<NegotiationMapper, Negotiation> implements NegotiationService {

    private final UsersService usersService;

    private final CustomersService customersService;

    private final EmployeesService employeesService;

    /**
     * 新增数据
     *
     * @param negotiation 实例对象
     * @return 实例对象
     */
    @Override
    public R<String> insert(Negotiation negotiation) {
        Customers customer = customersService.lambdaQuery().eq(Customers::getCustomerId, negotiation.getCustomerId()).one();
        negotiation.setCustomerName(customer.getName());
        Employees employees = employeesService.lambdaQuery().eq(Employees::getEmployeeId, negotiation.getEmployeeId()).one();
        negotiation.setEmployeeName(employees.getName());
        baseMapper.insert(negotiation);
        return R.ok(SAVE_OK_MSG);
    }

    /**
     * 修改数据
     *
     * @param negotiation 实例对象
     * @return 实例对象
     */
    @Override
    public R<String> update(Negotiation negotiation) {

        return R.ok(EDIT_OK_MSG);
    }

    /**
     * 通过主键删除数据
     *
     * @param negotiationId 主键
     * @return 是否成功
     */
    @Override
    public R<String> deleteById(Long negotiationId) {
        boolean exists = lambdaQuery().eq(Negotiation::getNegotiationId, negotiationId).exists();
        if (!exists) {
            return R.failed(ID_ERROR);
        }
        Long userId = UserHolder.getUser().getUserId();
        Users user = usersService.lambdaQuery().eq(Users::getUserId, userId).one();
        if (user.getRoleId() != ADMIN_ID) {
            return R.failed(DISALLOWED_OPERATION);
        }
        baseMapper.deleteById(negotiationId);
        return R.ok(DEL_OK_MSG);
    }

    @Override
    public R<Page<Negotiation>> getPage(PageDto<Negotiation> page, NegotiationPageDto dto) {
        EmployeeData employeeData = usersService.getEmployeeData();
        lambdaQuery()
                .eq(dto.getCustomerId() != null, Negotiation::getCustomerId, dto.getCustomerId())
                .eq(!employeeData.getIsAll(), Negotiation::getCreateUser, employeeData.getUserId())
                .eq(dto.getEmployeeId() != null, Negotiation::getEmployeeId, dto.getEmployeeId())
                .ge(dto.getStartTime() != null, Negotiation::getNegotiationTime, dto.getStartTime())
                .le(dto.getEndTime() != null, Negotiation::getNegotiationTime, dto.getEndTime())
                .page(page);

        return R.ok(page, GET_INFO_OK);
    }
}
